<?
	class _Uploader
	{
		var $File;
		var $FileName;	// New File Name if Random
		var $Name;		// Existing File Name
		var $Path;
		var $Widht;
		var $Height;
		var $FileType;
		var $FileSize;
		var $TempFile;
		var $Ext;
		var $AllowExt = array();
		
		function _Uploader($_Path = '')
		{
			$this->File = "";
			$this->FileName = "";
			$this->Width = "";
			$this->Height = "";
			$this->Path = $_Path;
		
		}
		function SetAllowedExts($Exts)
		{
			foreach($Exts as $Index => $Ext)
				array_push($this->AllowExt,strtolower($Ext));
		}
		function SetFile($File)
		{
			$this->File = $File;
			$Name = explode(".",$File['name']);
			$Ext = strtolower($Name[1]);
			$this->FileName = time().".".$Ext;
			$this->FileType = $File['type'];
			$this->Name 	= $File['name'];
			$this->Size 	= $File['size'];
			$this->TempFile  	= $File['tmp_name'];
			$Ext = explode ('.', $this->Name);
   			$this->Ext = $Ext[count($Ext)-1];
			list($this->Width, $this->Height) = getimagesize($this->TempFile);
		}
		function GetSize()
		{
			return $this->Size;
		}
		function GetWidth()
		{
			return $this->Width;
		}
		function GetHeight()
		{
			return $this->Height;
		}
		function SetPath($_Path)
		{
			$this->Path = $_Path;
		}
		function SetName($ImageName)
		{
			$this->FileName = $ImageName.".".$this->Ext; 
			
		}
		function Upload()
		{
			if($this->isValidExt($this->Ext))
			{
				copy($this->File['tmp_name'],$this->Path."/".$this->FileName) or 
					die("Error: File is not uploaded");
				return true;
			}
			else
			{
			//echo 'ext tyhpe '.$this->Ext;
				echo "Fatal Error: 	nvalid File Type";
				//return;
				exit;
			}
		}
		function isValidExt($Ext)
		{
			if(count($this->AllowExt) < 0)
				return true;
			foreach($this->AllowExt as $Index => $ValidExt)
			{
				if(strtolower($ValidExt) == strtolower($Ext))
					return true;
			}
			return false;
		}
		function DeleteFile($FileName)
		{
			if(file_exists($this->Path."/".$FileName) && $FileName != '')
				unlink($this->Path."/".$FileName);
			else
				return false;
		}
		/*function DeleteFile($FileName)
		{
			if(file_exists($FileName))
				unlink($FileName);
			else
				return false;
		}*/
		function UploadReSize($Width, $Height)
		{
		   
		   if($this->FileType == "image/pjpeg" || $this->FileType == "image/jpeg"){
		   		$NewImage = imagecreatefromjpeg($this->TempFile);
	   	   }
		   elseif($this->FileType == "image/x-png" || $this->FileType == "image/png"){
		   		$NewImage = imagecreatefrompng($this->TempFile);
	   	   }
		   elseif($this->FileType == "image/gif"){
		   	$NewImage = imagecreatefromgif($this->TempFile);
	   	   }
		   
		   $Ratio = $Width/$Height;
		   
		   if ($Ratio > 1)
		   {
			  $NewWidth = $Width;
			  $NewHeight = $Height;//$Height;  
		   }
		   else
		   {
				 $NewHeight = $Width;
				 $NewWidth =  $Height; 
		   }
		   //Resizing
		   if (function_exists(imagecreatetruecolor))
		   {
				$ReSizedImage = imagecreatetruecolor($NewWidth, $NewHeight); 
		   }
		   else
		   {
				die("Error: Please make sure you have GD library ver 2+");
		   }
		   imagecopyresized($ReSizedImage, $NewImage, 0, 0, 0, 0, $NewWidth, $NewHeight, $this->Width, $this->Height);
		  /****************** For Cropping Use Following **********************
		   imagecopyresized($ReSizedImage, $NewImage, 0, 0, 0, 0, $NewWidth, $NewHeight, $NewWidth, $NewHeight); 
	   	  /********************************************************************/
		   
		   ImageJpeg ($ReSizedImage,$this->Path."/".$this->FileName);
	   	   ImageDestroy ($ReSizedImage);
	   	   ImageDestroy ($NewImage);	  
		   return true;
		   
	  }
	function ResetUploader()
	{
		$this->File = "";
		$this->FileName = "";
		$this->Path 	= "";
		$this->FileType = "";
		$this->Name 	= "";
		$this->Size 	= "";
		$this->TempFile	= "";
   		$this->Ext 		= "";
	}
	
}	// End Class
	/*************************************************************************************
function fileUpload_thumb($filename,$largimg,$smallimg,$width,$height)
{
//$_FILES[filename][]
//make sure this directory is writable!
		//large Image Save
		//$picture1 = trim($filename['name']);
		//$picture1=rand(1000,9999).$picture1;		
//		copy($_FILES['file1']['tmp_name'],"picture/".$picture1);
	
	$path_thumbs = $smallimg;
	$path_big = $largimg;
	//the new width of the resized image, in pixels.
	$img_thumb_width = $width; //
	$img_thumb_height = $height;
	$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
	//List of allowed extensions if extlimit = yes
	$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
	
	//the image -> variables
	$file_type = $filename['type'];
	$file_name = $filename['name'];
	$file_size = $filename['size'];
	$file_tmp  = $filename['tmp_name'];
	//check if you have selected a file.
	if(!is_uploaded_file($file_tmp)){
	  echo "Error: Please select a file size upto 2 mb!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
	  exit(); //exit the script and don't process the rest of it!
	}
   //check the file's extension
   $ext = strrchr($file_name,'.');
   $ext = strtolower($ext);
   //uh-oh! the file extension is not allowed!
   if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
	  echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
	  exit();
   }
   //so, whats the file's extension?
   $getExt = explode ('.', $file_name);
   $file_ext = $getExt[count($getExt)-1];
   //create a random file name
   $rand_name = md5(time());
   $rand_name= rand(0,999999999);
   //the new width variable
   $ThumbWidth = $img_thumb_width;
   $ThumbHeight = $img_thumb_height;
   //////////////////////////
   // CREATE THE THUMBNAIL //
   //////////////////////////
   
   //keep image type
   if($file_size){
	  if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
		   $new_img = imagecreatefromjpeg($file_tmp);
	   }elseif($file_type == "image/x-png" || $file_type == "image/png"){
		   $new_img = imagecreatefrompng($file_tmp);
	   }elseif($file_type == "image/gif"){
		   $new_img = imagecreatefromgif($file_tmp);
	   }
	   //list the width and height and keep the height ratio.
	   list($width, $height) = getimagesize($file_tmp);
	   //calculate the image ratio
	   $imgratio=$width/$height;
	   
	   if ($imgratio>1){
		  $newwidth = $ThumbWidth;
		  $newheight = $ThumbHeight;  //change//$ThumbWidth/$imgratio;
	   }else{
			 $newheight = $ThumbHeight;
			 $newwidth =  $ThumbWidth; //change//$ThumbWidth*$imgratio;
	   }
	   //function for resize image.
	   if (function_exists(imagecreatetruecolor)){
	   $resized_img = imagecreatetruecolor($newwidth,$newheight);
	   }else{
			 die("Error: Please make sure you have GD library ver 2+");
	   }
	   //the resizing is going on here!
	  imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	   //finally, save the image
	   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
	   ImageDestroy ($resized_img);
	   ImageDestroy ($new_img);	   
	}
	//ok copy the finished file to the thumbnail directory		
	move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
	$filename = $rand_name.".".$file_ext; //thumbnail picture store
	return $filename;
}
function up_img($filename)
{
	if($filename['name'] !='') 
	{			
		//$filename = $_FILES['imgfile1'];
		$largimg = "./hotelpic";
		$smallimg = "./hotelpic/thumbs";
		$height = 100;
		$width = 100;
		$imgname = fileUpload_thumb($filename,$largimg,$smallimg,$width,$height);		
	}
	return $imgname;
}	
function adminup_img($filename)
{
	if($filename['name'] !='') 
	{			
		//$filename = $_FILES['imgfile1'];
		$largimg = "../hotelpic";
		$smallimg = "../hotelpic/thumbs";
		$height = 100;
		$width = 100;
		$imgname = fileUpload_thumb($filename,$largimg,$smallimg,$width,$height);		
	}
	return $imgname;
}
adminup_img($FILE);
/**************************************************************************************/
?>